//
//  ARTVersionUpdateHelper.m
//  ChartsDemo
//
//  Created by yunda on 2021/10/14.
//

#import "ARTVersionUpdateHelper.h"
#import "ARTAppUpdateView.h"

@implementation ARTVersionUpdateHelper

/// 检测是否有新版本
+ (void)detectionVersion{
    [ARTNetworkService request_checkAppVersion_WithParameter:@{} success:^(id  _Nullable json) {
//        J_UserDefault_SetObjectForKey(@"F", ART_Storage_ForcedUpdate);
        if ([json isKindOfClass:[NSDictionary class]]) {
            NSDictionary *data = (NSDictionary *)json;
            NSInteger updateFlag = [data j_integerForKey:@"updateFlag"];
            if (updateFlag == 1) {
                // 升级
//                NSInteger updateforceUpdateFlag = [data j_integerForKey:@"updateforceUpdateFlag"];
//                if (updateforceUpdateFlag == 1) {
                    // 强制升级
                NSString *version = [data j_stringForKey:@"version"];
                NSString *versionDescription = [data j_stringForKey:@"versionDescription"];
                [self gotoAppStoreUpdate:version message:versionDescription];
//                    J_UserDefault_SetObjectForKey(@"T", ART_Storage_ForcedUpdate);
//                }
            }
        }
    } failure:^(NSString * _Nonnull errorMSG) {
//        J_UserDefault_SetObjectForKey(@"F", ART_Storage_ForcedUpdate);
    }];
}

/// 检测是否是强制更新
//+ (void)detectForcedUpdate{
//    NSString *flg = J_UserDefault_GetObjectForKey(ART_Storage_ForcedUpdate);
//    if ([flg isEqualToString:@"T"]) {
//        [self detectionVersion];
//    }
//}

// 前去更新
+ (void)gotoAppStoreUpdate:(NSString *)title message:(NSString *)message{
    ARTAppUpdateView *view = [[ARTAppUpdateView alloc]initWithTitle:title message:message];
    view.okBlock = ^{
        NSURL *url = [NSURL URLWithString:kDownloadAPPURL];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    };
    [view show];
    NSLog(@"弹出更新");
}

@end
